
var xhr;

function browserLanguage() {
 var langloc = navigator.language ? navigator.language : navigator.userLanguage;
 var hyphen = langloc.indexOf('-');
 if (hyphen > 0) langloc = langloc.substring(0, hyphen + 1) + langloc.substring(hyphen + 1).toUpperCase();
 return langloc;
}

function initialize() {
 var langloc = browserLanguage();
 getRDF(langloc);
 localize(langloc);
}

function chglang(newloc) {
 getRDF(newloc);
 localize(newloc);
}

function getLabel(labelId) {
if (!xhr) alert("xhr is null");
while (xhr.readyState != 4) {
 alert("Localizing web page ... (readyState=" + xhr.readyState + ")");
}
if (!xhr.responseXML) alert("xhr.responseXML is null");
if (!xhr.responseXML.documentElement) alert("xhr.responseXML.documentElement is null");
 var xmldoc = xhr.responseXML.documentElement;
 var literals = xmldoc.getElementsByTagName("literal");
// var literals = xmldoc.getElementsByTagNameNS ? xmldoc.getElementsByTagNameNS("*", "literal") : xmldoc.getElementsByTagName("literal");
 for (var i = 0; i < literals.length; i ++) {
  var attribs = literals[i].attributes;
  for (var j = 0; j < attribs.length; j ++) {
   if (attribs[j].nodeName == "id") {
    if (attribs[j].value == labelId) {
     var lblelem = literals[i].getElementsByTagName("label")[0];
     return lblelem.firstChild.data;
    }
   }
  }
 }
 return "[&lt;literal id=\"" + labelId + "\"&gt; not found]";
}

function getRDF(langloc) {
 //call the right constructor for the browser being used
 if (window.ActiveXObject)
  xhr = new ActiveXObject("Microsoft.XMLHTTP");
 else if (window.XMLHttpRequest)
  xhr = new XMLHttpRequest();
 else
  alert("Localization of this page is not supported in your browser.");

// TO DO: try langloc first, then lang if 404 not found
 var hyphen = langloc.indexOf('-');
 var lang = hyphen < 0 ? langloc : langloc.substring(0, hyphen);

 //prepare the xmlhttprequest object
 xhr.open("GET", "/" + lang + "/literals.xml", true);
 xhr.setRequestHeader("Cache-Control", "no-cache");
 xhr.setRequestHeader("Pragma", "no-cache");
 xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
   if (xhr.status != 200)
    alert("HTTP error code " + xhr.status + " received: " + xhr.statusText);
   else if (xhr.responseText == null) {
     alert("Failed to retrieve XML file from the server - file not found.");
     return false;
   }
//else alert(xhr.responseText);
  }
 }

 //send the request
 xhr.send(null);
}

function rdfTopElemement(rdfxml) {
}

